package ro.dandanciu.webgallery.domainmodel;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import ro.dandanciu.webgallery.domainmodel.utils.DbConstants;

/**
 * Domain model class for galleries.
 * @author Dan Danciu
 */
@NamedQueries({
        @NamedQuery(name = "Gallery.findRootByOwner",
                query = "SELECT g FROM Gallery g WHERE g.name LIKE :filter "
                        + "and g.owner.userId LIKE :userId and g.parent is null"),
        @NamedQuery(name = "Gallery.findByOwnerAndParent",
                query = "SELECT g FROM Gallery g WHERE g.name LIKE :filter "
                        + "and g.owner.userId LIKE :userId and  g.parent.id = :galleryId "),
        @NamedQuery(name = "Gallery.findAllPublic",
                query = "SELECT g FROM Gallery g WHERE g.name LIKE :filter "
                        + "and g.publicAccess = true "),
        @NamedQuery(name = "Gallery.findByOwnerAndName",
                query = "SELECT g FROM Gallery g WHERE g.owner.userId = :ownerUserId "
                        + "and g.name = :galleryName "), })
@Entity
@Table(name = "TP_GALLERY")
public class Gallery {

    /**
     * The identifier of the gallery.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "K_GALLERY")
    private long id;

    /**
     * The name of the gallery.
     */
    @Column(name = "C_NAME", length = DbConstants.Gallery.NAME_SIZE)
    private String name;

    /**
     * A description of the gallery.
     */
    @Column(name = "C_DESCRIPTION", length = DbConstants.Gallery.DESCRIPTION_SIZE)
    private String description;

    /**
     * Parent gallery, if null, then is root gallery.
     */
    @ManyToOne
    @JoinColumn(name = "F_PARENT")
    private Gallery parent;

    /**
     * Owner of the gallery. The user which created the gallery.
     */
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "F_USER")
    private User owner;

    /**
     * Flag which indicates if the gallery is public.
     */
    @Column(name = "B_PUBLIC")
    private boolean publicAccess;

    /**
     * The list of sub-galleries contained by this gallery.
     */
    @OneToMany(cascade=CascadeType.ALL, targetEntity = Gallery.class, mappedBy = "parent")
    private List < Gallery > subGalleries;

    /**
     * The timestamp of the last time this item was updated. Mapped to
     * D_UPDATED.
     */
    @Column(name = "D_UPDATED")
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date updated;

    /**
     * The user id/software which has performed the last update. Mapped to
     * C_UPDATER.
     */
    @Column(name = "C_UPDATER", length = DbConstants.Generic.MODIFIED_SIZE)
    private String updater;

    /**
     * @return the id
     */
    public final long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public final void setId(final long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public final String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public final void setName(final String name) {
        this.name = name;
    }

    /**
     * @return the description
     */
    public final String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public final void setDescription(final String description) {
        this.description = description;
    }

    /**
     * @return the parent
     */
    public final Gallery getParent() {
        return parent;
    }

    /**
     * @param parent the parent to set
     */
    public final void setParent(final Gallery parent) {
        this.parent = parent;
    }

    /**
     * @return the owner
     */
    public final User getOwner() {
        return owner;
    }

    /**
     * @param owner the owner to set
     */
    public final void setOwner(final User owner) {
        this.owner = owner;
    }

    /**
     * @return the publicAccess
     */
    public final boolean isPublicAccess() {
        return publicAccess;
    }

    /**
     * @param publicAccess the publicAccess to set
     */
    public final void setPublicAccess(final boolean publicAccess) {
        this.publicAccess = publicAccess;
    }

    /**
     * @return the subGalleries
     */
    public final List < Gallery > getSubGalleries() {
        return subGalleries;
    }

    /**
     * @param subGalleries the subGalleries to set
     */
    public final void setSubGalleries(final List < Gallery > subGalleries) {
        this.subGalleries = subGalleries;
    }

    /**
     * @return the updated
     */
    public final Date getUpdated() {
        return updated;
    }

    /**
     * @param updated the updated to set
     */
    public final void setUpdated(final Date updated) {
        this.updated = updated;
    }

    /**
     * @return the updater
     */
    public final String getUpdater() {
        return updater;
    }

    /**
     * @param updater the updater to set
     */
    public final void setUpdater(final String updater) {
        this.updater = updater;
    }
}
